package cn.xn.hrms.api.controller;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.PageInfo;

import cn.xn.hrms.api.dto.res.UserRoleDTO;
import cn.xn.hrms.api.dto.req.PasswordUpdateReqDTO;
import cn.xn.hrms.api.dto.req.UserReqDTO;
import cn.xn.hrms.api.dto.req.UserRoleReqDTO;
import cn.xn.hrms.domain.config.JWTUtils;
import cn.xn.hrms.domain.service.UserService;
import cn.xn.hrms.domain.utils.ResultVo;
import cn.xn.hrms.infrastructure.dataobject.UserDO;
import io.swagger.annotations.ApiOperation;

/**
 * @author ysz
 */
@RestController
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户登录
     *
     * @param user
     * @return
     */
    @PostMapping("/user/login")
    @ApiOperation("用户登录")
    public ResultVo login(@RequestBody @Valid UserReqDTO user) {
        return ResultVo.success(userService.userLogin(user));
    }

    /**
     * 新增用户
     *
     * @param us
     * @return
     */
    @PostMapping("/user/register")
    @ApiOperation("新增用户")
    @RequiresRoles("admin")
    public ResultVo add(@RequestBody @Valid UserReqDTO us) {
        userService.create(us);
        return ResultVo.success();
    }

    /**
     * 根据登录token获取用户名
     *
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/user/name")
    @ApiOperation("根据登录token获取用户名")
    public ResultVo get(HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("Authorization");
        String username = JWTUtils.getUsername(token);
        return ResultVo.success(username);
    }

    /**
     * 分页查询用户列表
     *
     * @param page
     * @param size
     * @param name
     * @return
     */
    @GetMapping("/user/list")
    @ApiOperation("分页查询用户列表")
    @RequiresRoles("admin")
    public PageInfo<UserDO> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                 @RequestParam(value = "size", defaultValue = "10") Integer size,
                                 @RequestParam(value = "name",required = false) String name) {

        return userService.findList(page, size, name);
    }

    /**
     * "删除用户(支持批量）
     *
     * @param id
     * @return
     */
    @DeleteMapping("/user/delete")
    @ApiOperation("删除用户(支持批量）")
    @RequiresRoles("admin")
    public ResultVo deleteIdIn(@RequestBody Set<Integer> id) {
        userService.deleteByIdIn(id);
        return ResultVo.success();
    }

    /**
     * 修改用户
     *
     * @param id
     * @param us
     * @return
     */
    @PutMapping("/user/{id}")
    @ApiOperation("修改用户")
    @RequiresRoles("admin")
    public ResultVo update(@PathVariable Integer id, @RequestBody UserReqDTO us) {
        userService.update(id, us);
        return ResultVo.success();
    }

    /**
     * 用户修改密码
     *
     * @param form
     * @return
     */
    @ApiOperation("用户修改密码")
    @PutMapping("/user/update/password")
    public ResultVo updatePassword(@RequestBody PasswordUpdateReqDTO form) {
        userService.updatePassword(form.getOldPassword(), form.getNewPassword());
        return ResultVo.success();
    }

    /**
     * 用户分配角色
     *
     * @param userRoleReqDTO
     * @return
     */
    @ApiOperation("用户分配角色")
    @PostMapping("/user/saveRoles")
    public ResultVo saveUserRoles(@RequestBody UserRoleReqDTO userRoleReqDTO) {
        userService.saveRoles(userRoleReqDTO);
        return ResultVo.success();
    }

    /**
     * 查询用户角色
     *
     * @param userId
     * @return
     */
    @ApiOperation("查询用户角色")
    @GetMapping("/user/{userId}/getUserRoles")
    public List<UserRoleDTO> getUserRoles(@PathVariable Long userId) {
        return userService.getUserRoles(userId);
    }

    /**
     * 校验用户token有效性
     *
     * @return
     */
    @GetMapping("/user/token/check")
    @ApiOperation("用户token校验")
    public ResultVo userTokenCheck() {
        // 无需任何操作，鉴权逻辑会处理
        return ResultVo.success();
    }

}
